package my;

import my.file.FileInfo;
import my.file.Files;
import my.map.Map;
import my.map.TreeMap;
import my.set.Set;
import my.set.TreeSet;

/**
 * <p>
 * 测试
 * </p>
 *
 * @author AJun
 * @since 2020/8/31
 */
public class Main11 {

    public static void main(String[] args) {
        // testTreeMap1();
        // testTreeMap();
        testTreeSet();
    }

    /**
     * TreeMap实现的TreeSet
     */
    static void testTreeSet() {
        Set<String> set = new TreeSet<>();
        set.add("C");
        set.add("B");
        set.add("A");
        set.add("D");
        set.add("E");
        set.add("C");
        set.traversal(new Set.Visitor<String>() {
            @Override
            public boolean visit(String element) {
                System.out.println(element);
                return false;
            }
        });
    }

    /**
     * 用Set统计jdk的src源码中有多少个单词
     */
    static void testTreeMap() {
        FileInfo fileInfo = Files.read("E:\\environment\\java\\src\\java\\util\\concurrent",
                new String[]{"java"});
        System.out.println("文件数量: " + fileInfo.getFiles());
        System.out.println("代码行数: " + fileInfo.getLines());
        System.out.println("单词数量(没有去重): " + fileInfo.words().length);

        Map<String, Integer> map = new TreeMap<>();
        for (String word : fileInfo.words()) {
            Integer count = map.get(word);
            count = (count == null ? 0 : count);
            map.put(word, count + 1);
        }
        map.traversal(new Map.Visitor<String, Integer>() {
            @Override
            public boolean visit(String key, Integer value) {
                System.out.println("word=" + key + ", count=" + value);
                return false;
            }
        });
        System.out.println(map.size());
    }

    static void testTreeMap1() {
        Map<String, Integer> map = new TreeMap<>();
        map.put("public", 5);
        map.put("class", 2);
        map.put("text", 6);
        map.put("public", 8);
        map.put("delete", 8);

        map.remove("delete");
        System.out.println(map.containsKey("text"));
        System.out.println(map.containsValue(8));
        System.out.println(map.get("text"));

        map.traversal(new Map.Visitor<String, Integer>() {
            @Override
            public boolean visit(String key, Integer value) {
                System.out.println("key=" + key + ", value=" + value);
                return false;
            }
        });
    }

}
